package com.markyao.aop;

import com.alibaba.fastjson.JSON;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class TimeAspect {
    //切面类
    @Pointcut("@annotation(com.markyao.aop.LogAnnotation)")
    public void pt(){}//pt方法绑定一个注解，让注解生效


    //环绕pt方法
    @Around("pt()")
//    @Around(("execution(* com.markyao.service.impl.*.*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        //记录当前时间
        long start=System.currentTimeMillis();
        //调用目标方法
        Object result = pjp.proceed();
        //记录当前时间
        long end=System.currentTimeMillis();

        //从使用注解的方法上获取Module以及operation
        System.out.println("-------------------log start------------------------");
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        LogAnnotation logAnnotation = method.getAnnotation(LogAnnotation.class);
        String module = logAnnotation.module();
        String operation = logAnnotation.operation();
        System.out.println("module: "+module);
        System.out.println("operation: "+operation);
        String className = pjp.getTarget().getClass().getName();
        String methodName = signature.getName();
        System.out.println("className: "+className);
        System.out.println("methodName: "+methodName);
//        Object[] args = pjp.getArgs();
//        System.out.println("参数:  "+ args.toString());
        System.out.println("耗时:"+(end-start)+"ms");
        System.out.println("-------------------log end------------------------");
        return result;
    }

}
